/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils
{
	import flash.utils.getQualifiedClassName;
	
	public class Pooling extends Object
	{
		private static const _object:Object = new Object();
		private static var _blocker:Singleton;
		private const POOL:Array = new Array();
		private var _cls:Class;
		public function Pooling(blocker:Singleton, cls:Class) {
			_cls = cls;
		}
		
		public static function getInstance(cls:Class,weak:Boolean=false):Pooling {
			if (_object[cls] == null) {
				if (weak) {
					return null;
				}
				_object[cls] = new Pooling(_blocker,cls);
			}
			return _object[cls];
		}
		
		public static function cleanUp():void {
			var pooling:Pooling;
			for(var label:String in _object) {
				pooling = _object[label];
				if (pooling != null) {
					pooling.empty();
					delete _object[label];
				}
			}
		}

		public function checkOut():* {
			if (POOL.length == 0) {
				return new _cls();
			}
			return POOL.shift();
		}
		
		public function checkIn(value:*):void {
			if (value is _cls) {
				POOL.push(value);
			}
		}

		protected function empty():void {
			while(POOL.length > 0) {
				POOL.pop();
			}
		}		
	}

}

internal class Singleton {
}

